package cn.hxth.dubbo.dataaggr;


import com.alibaba.fastjson.JSONObject;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@Component  
@RabbitListener(queues = "aggr-data-change-queue")  
public class AggrDataChangeQueueReceiver {
	@Resource
private HttpHeaders headers ;
	@Resource(name = "myRedisTemplate")
	private RedisTemplate<String,Object> redisTemplate ;
	@Resource
	private RestTemplate restTemplate;

	@RabbitHandler
    public void process(String message) {  
    	JSONObject messageJSONObject = JSONObject.parseObject(message);
    	
    	String dimType = messageJSONObject.getString("dim_type");  
    	
    	if("plant".equals(dimType)) {
    		processPlantDimDataChange(messageJSONObject);
    	} else if("consumer".equals(dimType)) {
    		processConsumerDimDataChange(messageJSONObject);
    	} else if("product".equals(dimType)) {
    		processProductDimDataChange(messageJSONObject); 
    	}
    	/*else if("product_intro".equals(dimType)) {
    		processProductIntroDimDataChange(messageJSONObject);
    	}*/
	}
    
    private void processPlantDimDataChange(JSONObject messageJSONObject) {
    	Long id = messageJSONObject.getLong("cid");
    	


    	String dataJSON = (String) this.redisTemplate.opsForValue().get("plant_" + id);
    	
    	if(dataJSON != null && !"".equals(dataJSON)) {
    		this.redisTemplate.opsForValue().set("dim_plant_" + id, dataJSON);
    	} else {
    		this.redisTemplate.delete("dim_plant_" + id);
    	}
    }
    
    private void processConsumerDimDataChange(JSONObject messageJSONObject) {
    	Long id = messageJSONObject.getLong("cid");

    	String dataJSON = (String) this.redisTemplate.opsForValue().get("consumer_" + id);
    	
    	if(dataJSON != null && !"".equals(dataJSON)) {
    		this.redisTemplate.opsForValue().set("dim_consumer_" + id, dataJSON);
    	} else {
    		this.redisTemplate.delete("dim_consumer_" + id);
    	}
    }
    
    /*private void processProductIntroDimDataChange(JSONObject messageJSONObject) {
    	Long id = messageJSONObject.getLong("id");  
    	
    	Jedis jedis = jedisPool.getResource();
    	
    	String dataJSON = jedis.get("product_intro_" + id);
    	
    	if(dataJSON != null && !"".equals(dataJSON)) {
    		jedis.set("dim_product_intro_" + id, dataJSON);
    	} else {
    		jedis.del("dim_product_intro_" + id);
    	}
    }*/
    
    private void processProductDimDataChange(JSONObject messageJSONObject) {
    	Long id = messageJSONObject.getLong("pid");
    	

    	
    	String productDataJSON = (String) this.redisTemplate.opsForValue().get("product_" + id);
    	
    	if(productDataJSON != null && !"".equals(productDataJSON)) {
    		JSONObject productDataJSONObject = JSONObject.parseObject(productDataJSON);
			String productBaseInfoDataJSON = null ;
			long startTime = System.currentTimeMillis() ;
			long waitTime = 0l ;
			long endTime = 0l ;

			while (true){
				productBaseInfoDataJSON	 = restTemplate.exchange("http://localhost:81/pages/product/getProductBaseInfo?pid="+id, HttpMethod.GET,new HttpEntity<>(headers) ,String.class).getBody() ;
			if(productBaseInfoDataJSON!=null){
				break;
			}

				try {
					Thread.sleep(200);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				if(productBaseInfoDataJSON!=null)
					break;
				endTime = System.currentTimeMillis() ;
				waitTime = endTime - startTime ;
			}

    		if(productBaseInfoDataJSON != null && !"".equals(productBaseInfoDataJSON)) {
    			productDataJSONObject.put("product_base_info", JSONObject.parse(productBaseInfoDataJSON));
    		}
    		this.redisTemplate.opsForValue().set("dim_product_" + id, productDataJSONObject.toJSONString());
    	} else {
    		this.redisTemplate.delete("dim_product_" + id);
    	}
    }
  
}  